<?php

declare(strict_types=1);

namespace App\Middleware;

use App\Model\Manager;
use Hyperf\HttpServer\Contract\RequestInterface;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Hyperf\HttpServer\Contract\ResponseInterface as HttpResponse;

class ManagerRights implements MiddlewareInterface
{
    public function __construct(ContainerInterface $container, HttpResponse $response, RequestInterface $request)
    {
        $this->container = $container;
        $this->response = $response;
        $this->pathInfo = $request->getPathInfo();
    }

    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        /** @var Manager $manager */
        $manager = auth('manager')->user();
        if ($manager->checkRights($this->pathInfo)) {
            return $handler->handle($request);
        }

        return $this->response->json(
            [
                'status' => 403,
                'message' => '权限不足',
                'data' => [],
            ]
        );
    }
}